<?php
/**
 * 社团
 */
namespace backend\controllers;

use bc\controller\BackendController;
use backend\models\organization\OrganizationModel;
use backend\models\organization\ActivityModel;
use bc\util\backend\Page;
use bc\util\Uploader;

class OrganizationActivityController extends BackendController
{
	
	/**
	 * 社团活动统一需带的社团id
	 * @var int
	 */
	private $organizationId;
	
	public function beforeAction($action)
	{
		$organizationId = $this->get('organization_id');
		if(!$organizationId)
			$this->redirect('/');
		
		$this->organizationId = $organizationId;
		return parent::beforeAction($action);
	}
	
	
	/**
	 * 列表页
	 */
	public function actionIndex()
	{
		$model = new ActivityModel();
		
		$activityName = $this->get('activity_name', '');
		
		$params = 'organization_id = '.$this->organizationId;
		
		if($activityName != '')
		{
			$params = "`activity_name` LIKE '%{$activityName}%'";
		}
		
		$page = $this->get('page', 1);
		$model->sort = 'activity_id';
		$list = $model->getList($params, $page, self::PAGE_SIZE);
		
		$total = $model->getTotal($params);
		
		$pagination = new Page($total, self::PAGE_SIZE);
		
		$view['list'] = $list;
		$view['pagination'] = $pagination;
		
		$view['activity_name'] = $activityName;
		$view['organization_id'] = $this->organizationId;
		
		return $this->render('index', $view);
	}
	
	/**
	 * 新增
	 */
	public function actionAdd()
	{
		$view['organization_id'] = $this->organizationId;
		return $this->render('add', $view);	
	}
	
	
	/**
	 * 编辑
	 */
	public function actionEdit()
	{
		$id = $this->get('id');
		if(!$id)
			$this->redirect('/');
		
		$model = new ActivityModel();
		$row = $model->getRow(['activity_id' => $id]);
		
		if(!$row)
			$this->redirect('/');
		
		$view['row'] = $row;
		$view['organization_id'] = $this->organizationId;
		
		return $this->render('edit', $view);
	}
	
	public function actionSave()
	{
		if($this->getRequest()->getMethod() === 'POST' )
		{
			$model = new ActivityModel();
			$id = $this->post('id', 0);
			
			
			$data['organization_id'] = $this->organizationId;
			$data['activity_time'] = $this->post('activity_time', '');
			$data['activity_desc'] = $this->post('activity_desc', '');
			$data['activity_name'] = $this->post('activity_name', '');
			$data['link'] = $this->post('link', '');
			$data['status'] = $this->post('status', '1');
			
			
			//社团活动图片
			if($_FILES['image'])
			{
				$up = new Uploader( 'image' , $this->getUploadConfig() );
				$info = $up->getFileInfo();
				if($info['state'] === 'SUCCESS')
				{
					$data['image'] = str_replace(UPLOAD_PATH, '', $info['url']);
				}
			}
			
			
			if($id)
			{
				$model->modify($data, $id);
			}
			else
			{
				$model->add($data);
			}
			$output['msg'] = '操作成功!';
			
		}
		else 
		{
			$output['msg'] = '非法请求!';
		}
		$output['url'] = '/organization-activity/index?organization_id='.$this->organizationId;
		$this->forward($output);
	}
	
	
	/**
	 * @return array
	 */
	private function getUploadConfig()
	{
		//上传文件目录
		$path = UPLOAD_PATH . 'upload/';
		//上传配置
		$config = array(
				"savePath" => $path ,             //存储文件夹
				"maxSize" => 100000 ,                   //允许的文件最大尺寸，单位KB
				"allowFiles" => array( ".gif" , ".png" , ".jpg" , ".jpeg" , ".bmp" )  //允许的文件格式
		);
			
		//背景保存在临时目录中
		$config[ "savePath" ] = $path;
		return $config;
	}
}

